package com.ruoyi.caigou.service.impl;

import java.util.List;

import com.ruoyi.caigou.domain.BuyComparePriceGoods;
import com.ruoyi.caigou.domain.dto.BuyComDTO;
import com.ruoyi.caigou.mapper.BuyComparePriceGoodsMapper;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.caigou.mapper.BuyComparePriceMapper;
import com.ruoyi.caigou.domain.BuyComparePrice;
import com.ruoyi.caigou.service.IBuyComparePriceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购比价Service业务层处理
 * 
 * @author lvql
 * @date 2024-11-01
 */
@Service
public class BuyComparePriceServiceImpl implements IBuyComparePriceService 
{
    @Autowired
    private BuyComparePriceMapper buyComparePriceMapper;

    @Autowired
    private BuyComparePriceGoodsMapper buyComparePriceGoodsMapper;
    /**
     * 查询采购比价
     * 
     * @param id 采购比价主键
     * @return 采购比价
     */
    @Override
    public BuyComparePrice selectBuyComparePriceById(Integer id)
    {
        return buyComparePriceMapper.selectBuyComparePriceById(id);
    }

    @Override
    public BuyComDTO selectBuyComparePriceInfoById(Integer id) {
        BuyComparePrice buyComparePrice = buyComparePriceMapper.selectBuyComparePriceById(id);
        BuyComDTO buyComDTO = new BuyComDTO();
        buyComDTO.setBuyComparePrice(buyComparePrice);
        String code = buyComparePrice.getCode();
        BuyComparePriceGoods buyComparePriceGoods = new BuyComparePriceGoods();
        buyComparePriceGoods.setComparePriceCode(code);
        List<BuyComparePriceGoods> buyComparePriceGoods1 = buyComparePriceGoodsMapper.selectBuyComparePriceGoodsList(buyComparePriceGoods);
        buyComDTO.setBuyComparePriceGoods(buyComparePriceGoods1);
        return buyComDTO;
    }

    /**
     * 查询采购比价列表
     * 
     * @param buyComparePrice 采购比价
     * @return 采购比价
     */
    @Override
    public List<BuyComparePrice> selectBuyComparePriceList(BuyComparePrice buyComparePrice)
    {
        return buyComparePriceMapper.selectBuyComparePriceList(buyComparePrice);
    }

    /**
     * 新增采购比价
     * 
     * @param buyComparePrice 采购比价
     * @return 结果
     */
    @Override
    public int insertBuyComparePrice(BuyComparePrice buyComparePrice)
    {
        buyComparePrice.setCreateTime(DateUtils.getNowDate());
        return buyComparePriceMapper.insertBuyComparePrice(buyComparePrice);
    }

    @Override
    public int insertBuyComparePricePlus(BuyComDTO buyComDTO) {
        BuyComparePrice buyComparePrice = buyComDTO.getBuyComparePrice();
        List<BuyComparePriceGoods> buyComparePriceGoods = buyComDTO.getBuyComparePriceGoods();
        for (BuyComparePriceGoods buyComparePriceGood : buyComparePriceGoods) {
            buyComparePriceGood.setComparePriceCode(buyComparePrice.getCode());
            buyComparePriceGoodsMapper.insertBuyComparePriceGoods(buyComparePriceGood);
        }
        return buyComparePriceMapper.insertBuyComparePrice(buyComparePrice);
    }

    /**
     * 修改采购比价
     * 
     * @param buyComparePrice 采购比价
     * @return 结果
     */
    @Override
    public int updateBuyComparePrice(BuyComparePrice buyComparePrice)
    {
        buyComparePrice.setUpdateTime(DateUtils.getNowDate());
        return buyComparePriceMapper.updateBuyComparePrice(buyComparePrice);
    }

    @Transactional
    @Override
    public int updateBuyComparePricePlus(BuyComDTO buyComDTO) {
        BuyComparePrice buyComparePrice = buyComDTO.getBuyComparePrice();
        String code = buyComparePrice.getCode();
        buyComparePriceGoodsMapper.deleteBuyComparePriceGoodsByComparePriceCode(code);
        buyComparePriceMapper.deleteBuyComparePriceById(buyComparePrice.getId());
        return insertBuyComparePricePlus(buyComDTO);
    }

    /**
     * 批量删除采购比价
     * 
     * @param ids 需要删除的采购比价主键
     * @return 结果
     */
    @Override
    public int deleteBuyComparePriceByIds(Integer[] ids)
    {
        return buyComparePriceMapper.deleteBuyComparePriceByIds(ids);
    }

    @Override
    public int deleteBuyComparePriceInfoByIds(Integer[] ids) {
        for (Integer id : ids) {
            BuyComparePrice buyComparePrice = buyComparePriceMapper.selectBuyComparePriceById(id);
            String code = buyComparePrice.getCode();
            buyComparePriceGoodsMapper.deleteBuyComparePriceGoodsByComparePriceCode(code);
        }
        return buyComparePriceMapper.deleteBuyComparePriceByIds(ids);
    }
    /**
     * 删除采购比价信息
     * 
     * @param id 采购比价主键
     * @return 结果
     */
    @Override
    public int deleteBuyComparePriceById(Integer id)
    {
        return buyComparePriceMapper.deleteBuyComparePriceById(id);
    }


}
